package dao;

import model.Goods;
import model.In;
import model.Out;
import model.Ware;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class Dao_warehousing extends Dao{
    //出库明细
    public static Ware select_All(String wareID)
    {
        Ware ware = new Ware();
        String sql = "select distinct * from warehouse_1 where i_no = '" + wareID + "'";
        ResultSet re = executeQuery(sql);
        try {

            if(re.next())
            {


                ware.setWareID(re.getString("i_no"));
                ware.setAddress(re.getString("s_address"));
                ware.setsPhone(re.getString("s_phone"));
                ware.setSupplierID(re.getString("s_no"));
                ware.setSupplierMan(re.getString("s_people"));
                ware.setTime(re.getString("i_time"));
                ware.setSupplierName(re.getString("s_name"));

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return ware;
    }

    public static List select_All_1()
    {
        List list = new ArrayList();
        String sql = "select distinct * from warehouse";

        ResultSet re = executeQuery(sql);
        try {
            while (re.next())
            {
                Ware ware = new Ware();

                ware.setWareID(re.getString("i_no"));
                ware.setAddress(re.getString("s_address"));
                ware.setReal(re.getDouble("i_real"));
                ware.setsPhone(re.getString("s_phone"));
                ware.setDiscounts(re.getDouble("discounts"));
                ware.setSupplierID(re.getString("s_no"));
                ware.setSupplierMan(re.getString("s_people"));
                ware.setTime(re.getString("i_time"));
                ware.setTotal(re.getDouble("i_total"));
                ware.setSupplierName(re.getString("s_name"));

                list.add(ware);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    public static Vector<String> getWareID()
    {
        List list = Dao_warehousing.select_All_1();
        Vector<String> WareID = new Vector<>();

        for(int i = 0; i < list.size(); i++)
        {
            Ware ware = (Ware) list.get(i);
            WareID.add(ware.getWareID());
        }

        return WareID;
    }

    //入库商品明细
    public static List select_goods(String wareID)
    {
        List list = new ArrayList();
        String sql = "select * from goods where i_no = '" + wareID + "'";

        ResultSet re = executeQuery(sql);
        try {
            while (re.next())
            {
                Goods ware = new Goods();

                ware.setWareID(re.getString("i_no"));
                ware.setGoodsID(re.getString("c_no"));
                ware.setReal(re.getDouble("ready"));
                ware.setGoodsName(re.getString("c_name"));
                ware.setType(re.getString("cs_name"));
                ware.setPrice(re.getDouble("price"));
                ware.setNumber(re.getInt("i_number"));
                ware.setDiscount(re.getDouble("discount"));
                ware.setTotal(re.getDouble("totals"));

                list.add(ware);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    //添加入库单信息
    public static int addIn(String i_no, String s_no, String i_discount, String i_total, String i_real)
    {
        int i = 0;
        try {

            String sql = "insert into in_repertory(i_no, s_no,i_discount, i_total, i_real) values('"+ i_no +"','"+ s_no +"','"+ i_discount +"','"+ i_total +"','"+ i_real +"')";
            i = Dao.exeUpdate(sql);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return i;
    }

    //添加入库明细
    public static int add(String id_no, String c_no, String i_no, String i_number, String i_price, String i_realPrice, String i_time)
    {
        int i = 0;

        try {
            String sql = "insert into in_details(id_no, c_no, i_no, i_number, i_price, i_realprice, i_time) values('"+ id_no +"','"+ c_no +"','"+ i_no +"',"+ i_number +","+ i_price +"," + i_realPrice + ",'" + i_time + "')";
            i = Dao.exeUpdate(sql);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return i;
    }

    //删除入库明细
    public static int deleteDetail(String detailID) {
        int i = 0;
        try {
            String sql = "delete from  in_details where id_no = '" + detailID + "'";
            i = Dao.exeUpdate(sql);
            System.out.println(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Dao.close();

        return i;
    }

    //通过入库单编号获取修改视图所有商品
    public static List select_goods_4(String detailID)
    {
        List list = new ArrayList();
        String sql = "select * from goods_4 where i_no = '" + detailID + "'";

        ResultSet re = executeQuery(sql);
        try {
            while (re.next())
            {
                Goods ware = new Goods();

                ware.setDetailID(re.getString("id_no"));
                ware.setGoodsID(re.getString("c_no"));
                ware.setReal(re.getDouble("ready"));
                ware.setGoodsName(re.getString("c_name"));
                ware.setType(re.getString("cs_name"));
                ware.setPrice(re.getDouble("price"));
                ware.setNumber(re.getInt("i_number"));
                ware.setDiscount(re.getDouble("discount"));
                ware.setTotal(re.getDouble("totals"));

                list.add(ware);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }


    //修改入库明细数量
    public static int updateDetail(String detailID, String number, String i_price, String i_realPrice) {
        int i = 0;
        try {
            String sql = "update in_details set i_number = " + number + ", i_price = " + i_price + ", i_realprice = " + i_realPrice + " where id_no = '" + detailID + "'";
            i = Dao.exeUpdate(sql);
            System.out.println(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Dao.close();

        return i;
    }

    //删除入库单
    public static int delete_in(String i_no) {
        int i = 0;
        try {
            String sql = "delete from  in_repertory where i_no = '" + i_no + "'";
            i = Dao.exeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Dao.close();

        return i;
    }

    //获取所有出库单信息
    public static List select_inAll()
    {
        List list = new ArrayList();
        String sql = "select distinct * from in_repertory";

        ResultSet re = executeQuery(sql);
        try {
            while (re.next())
            {
                In in = new In();

                in.setI_no(re.getString("i_no"));
                in.setCl_no(re.getString("s_no"));
                in.setI_discount(re.getString("i_discount"));
                in.setI_total(re.getString("i_total"));
                in.setI_real(re.getString("i_real"));

                list.add(in);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    //获取入库单编号
    public static Vector<String> getInID()
    {
        List list = Dao_warehousing.select_inAll();
        Vector<String> WareID = new Vector<>();

        for(int i = 0; i < list.size(); i++)
        {
            In in = (In) list.get(i);
            WareID.add(in.getI_no());
        }

        return WareID;
    }

}
